<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();

header('Access-Control-Allow-Origin:*');

date_default_timezone_set("PRC");

header("Content-Type: text/html;charset=utf8");

require_once '../common/in_mysql.php';
require_once '../db/school.db.php';

$action = filter_input(INPUT_POST, "action") ? filter_input(INPUT_POST, "action") : filter_input(INPUT_GET, "action");

$res = array();
switch ($action) {
    case 'getSchool':
        $res = getSchool();
        break;

    case 'delZone':
        $res = delZone();
        break;
    case 'delSchool':
        $res = delSchool();
        break;
    case 'addSchool':
        $res = addSchool();
        break;
    case 'editSchool':
        $res = editSchool();
        break;
    case 'addRegion':
        $res = addRegion();
        break;
    case 'addCountry':
        $res = addCountry();
    default :
}
echo json_encode($res);

function getSchool()
{
    $schoolDb = new SchoolDb();
    $zoneList = $schoolDb->getZoneList();
    $schoolList = $schoolDb->getSchoolList();
    $zoneSchool = array();
    foreach ($schoolList as $school) {
        if (!isset($zoneSchool[$school['zone']])) {
            $zoneSchool[$school['zone']] = array();
        }
        $temp = $zoneSchool[$school['zone']];
        $sc['id'] = $school['scid'];
        $sc['name'] = $school['sname'];
        $sc['createTime'] = $school['ctime'];
        array_push($temp, $sc);
        $zoneSchool[$school['zone']] = $temp;
    }
    $schoolMap = array();
    foreach ($zoneList as $zone) {
        if ($zone['pzone'] == 0) {
            $schoolMap[$zone['zid']]['id'] = $zone['zid'];
            $schoolMap[$zone['zid']]['name'] = $zone['name'];
            $schoolMap[$zone['zid']]['type'] = $zone['type'];
            $schoolMap[$zone['zid']]['remark'] = $zone['remark'];
            $schoolMap[$zone['zid']]['children'] = array();
        } else {
            $temp = $schoolMap[$zone['pzone']]['children'];
            $t['id'] = $zone['zid'];
            $t['name'] = $zone['name'];
            $t['type'] = $zone['type'];
            $t['remark'] = $zone['remark'];
            $t['data'] = $zoneSchool[$t['id']];
            array_push($temp, $t);
            $schoolMap[$zone['pzone']]['children'] = $temp;
        }
    }
    $res['code'] = 0;
    $res['msg'] = "";
    $res['count'] = 12;
    $res['data'] = $schoolMap;
    return $res;
}

//删除国家
function delZone()
{
    $id = filter_input(INPUT_POST, "id");
    $type = filter_input(INPUT_POST, "type");
    $schoolDb = new SchoolDb();
    $idd = $id;


    switch ($type){
        case "0":
            $zone = $schoolDb->queryZone($id);
            if ($zone) {
                $res['code'] = 201;
                $res['msg'] = "无法删除，请先删除国家下的地区";
                return $res;
            } else {
                $data1 = $schoolDb->deleteZone($id);
                if($data1){
                    $res['code'] = 0;
                    $res['msg'] = "删除成功";
                    return $res;
                }
                else{
                    $res['code'] = 202;
                    $res['msg'] = "删除失败";
                    return $res;
                }
            }
            break;
        case "1":
            $schoolData=$schoolDb->querySchoolZone($id);
            if($schoolData){
                $res['code'] = 201;
                $res['msg'] = "无法删除，请先删除该区域中的学校";
                return $res;
            }
            else{
                $data1 = $schoolDb->deleteZone($id);
                if($data1){
                    $res['code'] = 0;
                    $res['msg'] = "删除成功";
                    return $res;
                }
                else{
                    $res['code'] = 202;
                    $res['msg'] = "删除失败";
                    return $res;
                }

            }


    }



//
//    if ($data1) {
//        $zone = $schoolDb->queryZone($id);
//        if ($zone)
//        {
//            $count = count($zone);
//            for ($i = 0; $i < $count; $i++) {
//                $id=$zone[$i]['zid'];
//                $data2 = $schoolDb->deleteZone($id);
//            }
//            if ($data2) {
//                $id=$idd;
//                $regionData = $schoolDb->querySchoolZone($id);
//
//                if ($regionData) {
//                    $regionCount = count($regionData);
//                    for ($j = 0; $j < $regionCount; $j++) {
//                        $id=$regionData[$j]['scid'];
//                      $data3=$schoolDb->deleteSchool($id);
//                    }
//                    if($data3){
//                        $res['code'] = 0;
//                        $res['msg'] = "删除成功";
//                        return $res;
//                    }
//
//                }
//                else {
//                    $res['code'] = 0;
//                    $res['msg'] = "删除成功";
//                    return $res;
//                }
//
//            }
//
//
//        }
//
//        else {
//
//            $res['code'] = 0;
//            $res['msg'] = "删除成功";
//            return $res;
//        }
//
//    } else {
//        $res['code'] = 206;
//        $res['msg'] = "删除失败";
//
//    }


}

//删除学校
function delSchool()
{
    $id = filter_input(INPUT_POST, "id");
    $schoolDb = new SchoolDb();

    if ($schoolDb->deleteSchool($id)) {
        $res['code'] = 0;
        $res['msg'] = "删除成功";
    } else {
        $res['code'] = 236;
        $res['msg'] = "删除失败";
    }
    return $res;
}

//新增学校
function addSchool()
{
    $id = filter_input(INPUT_POST, "zoneId");
    $name = filter_input(INPUT_POST, "name");
    $schoolDb = new SchoolDb();
    $data = $schoolDb->querySchool($name);
    $time = time();
    if (!$data) {
        if ($schoolDb->addSchool($id, $name, $time)) {
            $res['code'] = 0;
            $res['msg'] = "创建成功";
        } else {
            $res['code'] = 236;
            $res['msg'] = "创建失败";
        }
    } else {
        $res['code'] = 237;
        $res['msg'] = "创建失败，该学校已存在";
    }


    return $res;

}

//编辑学校
function editSchool()
{

    $id = filter_input(INPUT_POST, "schoolId");
    $name = filter_input(INPUT_POST, "name");
    $schoolDb = new SchoolDb();
    $data = $schoolDb->querySchool($name);

    if (!$data) {
        if ($schoolDb->editSchool($id, $name)) {
            $res['code'] = 0;
            $res['msg'] = "修改成功";
        } else {
            $res['code'] = 236;
            $res['msg'] = "修改失败";
        }
    } else {
        $res['code'] = 237;
        $res['msg'] = "编辑失败，该学校已存在或未做修改";
    }


    return $res;
}

//新增地区
function addRegion()
{
    $type = filter_input(INPUT_POST, "type");
    $name = filter_input(INPUT_POST, "name");
    $pzone = filter_input(INPUT_POST, "zid");
    $remark = filter_input(INPUT_POST, "remark");
    $schoolDb = new SchoolDb();
    if ($type == 0) {
        $type = 1;
    } else {
        $type = 0;
    }
    $data = $schoolDb->queryRegion($name);
    $time = time();
    if (!$data) {
        if ($schoolDb->addRegion($name, $type, $remark, $pzone, $time)) {
            $res['code'] = 0;
            $res['msg'] = "创建成功";
        } else {
            $res['code'] = 236;
            $res['msg'] = "创建失败";
        }
    } else {
        $res['code'] = 237;
        $res['msg'] = "创建失败，该学校已存在";
    }


    return $res;


}

//新增国家
function addCountry()
{
    $name = filter_input(INPUT_POST, "name");
    $remark = filter_input(INPUT_POST, "remark");
    $schoolDb = new SchoolDb();
    $type = 0;
    $pzone = 0;
    $data = $schoolDb->queryRegion($name);
    $time = time();
    if (!$data) {
        if ($schoolDb->addRegion($name, $type, $remark, $pzone, $time)) {
            $res['code'] = 0;
            $res['msg'] = "创建成功";
        } else {
            $res['code'] = 236;
            $res['msg'] = "创建失败";
        }
    } else {
        $res['code'] = 237;
        $res['msg'] = "创建失败，该国家已存在或国家与地区名称不能一致";
    }


    return $res;


}